import { createStore } from 'vuex'

const store = createStore({
  // 数据定义
  state () {
    return {
      user: {
        id: 0,
        nickname: '',
        avatar: '',
        phone: '',
        email: '',
        description: '',
        isLogin: false
      },
      hideHeader: false,
      hideFooter: false
    }
  },
  // 数据获取
  getters: {
    getHideHeader: state => {
      return state.hideHeader
    },
    getHideFooter: state => {
      return state.hideFooter
    },
    getUser: state => {
      // const isLogin = localStorage.getItem('mkcsa-user')
      // if ((!state.user.id || state.user.id === 0) && isLogin) {
      //   state.user.isLogin = isLogin
      // }
      return state.user
    }
  },
  /** mutations  vue规范的state数据更改
   使用方法:
   import { useStore } from 'vuex'
   const store = useStore()
   store.commit('increment')
   */
  mutations: {
    increment (state) {
      state.count++
    },
    setHideHeader (state, val) {
      state.hideHeader = val
    },
    setHideFooter (state, val) {
      state.hideFooter = val
    },
    setUser (state, val) {
      state.user = val
    },
    setLogin (state, val) {
      state.user.isLogin = val
    }
  },
  // 异步的更改数据实际是提交到mutations
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

export default store
